package com.jinzhu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jinzhu.bo.AddressBO;
import com.jinzhu.mapper.UserAddressMapper;
import com.jinzhu.pojo.UserAddress;
import com.jinzhu.service.AddressService;
import com.jinzhu.utils.YesOrNo;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 地址相关的api接口
 * JinZhu
 */
@Service
public class AddressServiceImpl implements AddressService {
    /**
     * 用户地址
     */
    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 全局唯一id
     */
    @Autowired
    private Sid sid;

    /**
     * @param userId
     * @return
     * @Transactional(propagation = Propagation.SUPPORTS)
     * 根据用户id查询收货地址列表
     * 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryAll(String userId) {
        return userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId, userId));
    }

    /**
     * @param addressBO
     * @return
     * @Transactional(propagation = Propagation.SUPPORTS)
     * 用户新增地址
     * 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public void addNewUserAddress(AddressBO addressBO) {
// 1. 判断当前用户是否存在地址，如果没有，则新增为‘默认地址’
        Integer isDefault = 0;
        List<UserAddress> addressList = this.queryAll(addressBO.getUserId());
        //list判断不为空 .isEmpty()  判断是否为默认地址
        if (addressList == null || addressList.isEmpty() || addressList.size() == 0) {
            isDefault = 1;
        }

        String addressId = sid.nextShort();

        // 2. 保存地址到数据库
        UserAddress newAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO, newAddress);

        newAddress.setId(addressId);
        newAddress.setIsDefault(isDefault);
        newAddress.setCreatedTime(new Date());
        newAddress.setUpdatedTime(new Date());

        userAddressMapper.insert(newAddress);
    }

    /**
     * @param addressBO
     * @return
     * @Transactional(propagation = Propagation.SUPPORTS)
     * 用户修改地址
     * 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public void updateUserAddress(AddressBO addressBO) {

        String addressId = addressBO.getAddressId();

        UserAddress pendingAddress = new UserAddress();
        BeanUtils.copyProperties(addressBO, pendingAddress);

        pendingAddress.setId(addressId);
        pendingAddress.setUpdatedTime(new Date());

        userAddressMapper.updateById(pendingAddress);
    }

    /**
     * 用户设置默认地址
     *
     * @param userId
     * @param addressId
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public void updateUserAddressToBeDefault(String userId, String addressId) {

        // 1. 查找默认地址，设置为不默认
        LambdaQueryWrapper<UserAddress> address = new LambdaQueryWrapper();
        address.eq(UserAddress::getUserId, userId);
        address.eq(UserAddress::getIsDefault, YesOrNo.YES.type);
        List<UserAddress> list = userAddressMapper.selectList(address);
        for (UserAddress ua : list) {
            ua.setIsDefault(YesOrNo.NO.type);
            userAddressMapper.updateById(ua);
        }

        // 2. 根据地址id修改为默认的地址
        UserAddress defaultAddress = new UserAddress();
        defaultAddress.setId(addressId);
        defaultAddress.setUserId(userId);
        defaultAddress.setIsDefault(YesOrNo.YES.type);
        userAddressMapper.updateById(defaultAddress);

    }

    @Override
    public UserAddress queryUserAddres(String userId, String addressId) {
        return null;
    }

    /**
     * @param userId
     * @return
     * @Transactional(propagation = Propagation.SUPPORTS)
     * 用户删除地址
     * 如果当前存在事务，则加入该事务；如果当前没有事务，则以非事务的方式继续运行。
     */
    @Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
    @Override
    public void deleteUserAddress(String userId, String addressId) {
        LambdaQueryWrapper<UserAddress> address = new LambdaQueryWrapper();
        address.eq(UserAddress::getId, addressId);
        address.eq(UserAddress::getUserId, userId);
        userAddressMapper.delete(address);
    }

}
